//
//  MainViewController.swift
//  大新浪微博
//
//  Created by 周太锋 on 16/2/22.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
        
        setupTabbar()
    }
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //将按钮设置到顶部
        tabBar.bringSubviewToFront(composeButton)
        
        //计算按钮宽度 ||  -1是防边界处理
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count) - 1
        
        composeButton.frame = CGRectInset(tabBar.bounds, width * 2, 0)
    }
    
    //mark: - 私有控件
    //撰写按钮
    private lazy var composeButton:UIButton = {
        
        let button = UIButton()
        
        let imageName = "tabbar_compose_icon_add"
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        
        let backImageName = "tabbar_compose_button"
        button.setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
        button.setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
    
        
        //让按钮根据背景图片调整大小
        button.sizeToFit()
        
        return button
    }()

}


//Mark: - 设置 tabbar
extension MainViewController {
    
    //设置 tabbar
    private func setupTabbar() {
        
        // 设置tabbar背景图片，以下两句代码必须连用，Nav 的处理类似
        // 以下两个属性都设置成 [[UIImage alloc] init] 就是完全透明
        // 如果提供图像，会自动拉伸
        tabBar.shadowImage = UIImage.cz_singleDotImage(UIColor(white: 0.9, alpha: 1.0))
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        
        //添加撰写按钮
        tabBar.addSubview(composeButton)
        
        // 添加监听方法
        composeButton.addTarget(self, action: "clickComposeButton", forControlEvents: .TouchUpInside)
    }
}


//通过 estension 把相关的代码进行分组放置
extension MainViewController {
    
    //添加所有子控制器
    private func addChildViewControllers() {
        
        addChildViewController(HomeViewController(),title: "首页",imageName: "tabbar_home")
        addChildViewController(MessageViewController(),title: "消息",imageName: "tabbar_message_center")
        
        //添加一个空白的控制器
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverViewController(),title: "发现",imageName: "tabbar_discover")
        addChildViewController(ProfileViewController(),title: "我",imageName: "tabbar_profile")
    }
    
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        
        
        vc.title = title
        
        //设置文本属性
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        //如果要设置文字大小，需要设置 normal
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: .Normal)

        vc.tabBarItem.image = UIImage(named: imageName)
        
        //默认情况下，tabbar 的 image 会使用系统默认颜色渲染
        //AlwaysOriginal渲染模式，直接使用素材，系统不再加工
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        
        
        let nav = UINavigationController(rootViewController: vc)
        
        //添加子控制器
        addChildViewController(nav)
    }
}